<?php
namespace app\manage\controller;

use app\common\base\BasicRest;
use app\manage\model\CatalogModel;
use think\Request;

class Catalog extends BasicRest
{
    public function __construct()
    {
        parent::__construct();
    }

    #获取字典表信息
    public function index(){
        $data = CatalogModel::where('IsDeleted',0)->select();
        if($data) return res_success($data);
        return res_fail('未找到信息');
    }

    #新增字典信息
    public function save(Request $request){
        $data = $request->post();
        $res = CatalogModel::create($data);
        if($res) return res_success();
        return res_fail('创建失败');
    }

    #修改字典信息
    public function update(Request $request,$id){
        $res = CatalogModel::where('Id',$id)->update($request->put());
        if($res) return res_success();
        return res_fail('修改失败');
    }

    #删除字典信息
    public function delete($id){
        $data = CatalogModel::get($id);
        $res = CatalogModel::where('Id',$id)->update(['IsDeleted'=>'1']);
        if($data['ParentId']==0) {
            $deleteInfo = CatalogModel::where('ParentId', $data->Id)->select();
            foreach ($deleteInfo as $info) {
                $info->IsDeleted = 1;
                $res = $info->save();
            }
        }
        if($res) return res_success();
        return res_fail('删除失败');
    }
}